package com.hzhc.controller.carteam;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.TowInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.TowInfoService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.TowInfoVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 拖挂信息 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-12-18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/towInfo")
public class TowInfoController extends BaseController {
    private final TowInfoService towInfoService;
    /**
     * 拖挂校验
     */
    @RequestMapping("/getTowNo")
    public ResultUtil<T> getTowNo(HttpServletRequest request, @RequestBody TowInfo towInfo){
        return towInfoService.getTowNo(towInfo);
    }

    /**
     * 拖挂信息列表
     * @return
     */
    @RequestMapping("/getTowInfoList")
    public ResultUtil<List<TowInfo>> getTowInfoList(@RequestBody TowInfoVo vo){
        Page<TowInfo> page =new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<TowInfo> list =towInfoService.getList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 拖挂信息新增
     */
    @RequestMapping("/saveTowInfo")
    @SysTemLog(operModul = "基础资料-拖挂添加", operType = "添加", operDesc = "拖挂添加功能", modular = "PC")
    public ResultUtil<T> saveTowInfo(HttpServletRequest httpServletRequest,@RequestBody TowInfo towInfo) {
        UserDto user= TokenUtil.getUser(httpServletRequest);
        towInfo.setUserId(user.getId());
        towInfo.setHdId(towInfo.getCompanyId().toString());
        return towInfoService.addRet(towInfo);
    }
    /**
     * 拖挂信息编辑
     */
    @RequestMapping("/editTowInfo")
    @SysTemLog(operModul = "基础资料-拖挂编辑", operType = "编辑", operDesc = "拖挂编辑功能", modular = "PC")
    public ResultUtil<T> editTowInfo(@RequestBody TowInfo towInfo){
        return towInfoService.updateRet(towInfo);
    }
    /**
     * 拖挂信息删除
     */
    @RequestMapping("/delTowInfo/{id}")
    @SysTemLog(operModul = "基础资料-拖挂删除", operType = "删除", operDesc = "拖挂删除功能", modular = "PC")
    public ResultUtil<T>  delTowInfo(@PathVariable("id") Long id) {
        return towInfoService.delTowInfo(new TowInfo().setId(id));
    }

    /**
     * 拖挂导入
     * 保存上传的拖挂
     */
    @PostMapping("/importTowList")
    public ResultUtil<T> importTowList(HttpServletRequest request, @RequestBody TowInfoVo vo) {
        return towInfoService.importTowList(request, vo.getTowList());
    }

    /**
     * 拖挂清单导出模板下载
     */
    private final static String EXPORT_EXCEL_PATH_CMS = "static/download/拖挂导入.xlsx";

    @GetMapping("/exportTowFile")
    public void exportTowFile(HttpServletRequest request, HttpServletResponse response) {
        towInfoService.exportTowFile(request, response, EXPORT_EXCEL_PATH_CMS);
    }

    /**
     *拖挂导入
     */
    @RequestMapping("/towYzsUpload")
    public ResultUtil<List<TowInfoVo>> towUpload(MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return towInfoService.towUpload(user,file,"YZS");
    }
}
